#!/bin/bash

# Flink集群部署脚本 - 最终版本

echo "=== Flink集群部署脚本 ==="

# 1. 编译打包项目
echo "1. 编译打包项目..."
mvn clean package -DskipTests

if [ $? -ne 0 ]; then
    echo "❌ 项目打包失败"
    exit 1
fi
echo "✅ 项目打包成功"

# 2. 检查JAR包
echo "2. 检查JAR包..."
JAR_FILE="target/datalake-1.0.0.jar"
if [ -f "$JAR_FILE" ]; then
    echo "✅ JAR包存在: $JAR_FILE"
    echo "JAR包大小: $(du -h $JAR_FILE | cut -f1)"
else
    echo "❌ JAR包不存在"
    exit 1
fi

# 3. 检查配置文件
echo "3. 检查配置文件..."
if [ -f "flink-config.yaml" ]; then
    echo "✅ Flink配置文件存在"
else
    echo "❌ Flink配置文件不存在"
    exit 1
fi

if [ -f "setup.sql" ]; then
    echo "✅ SQL脚本存在"
else
    echo "❌ SQL脚本不存在"
    exit 1
fi

# 4. 提供部署步骤
echo ""
echo "=== 部署步骤 ==="
echo ""
echo "1. 更新Flink配置文件:"
echo "   sudo cp flink-config.yaml /opt/flink/source/conf/config.yaml"
echo ""
echo "2. 重启Flink集群:"
echo "   sudo /opt/flink/source/bin/stop-cluster.sh"
echo "   sudo /opt/flink/source/bin/start-cluster.sh"
echo ""
echo "3. 等待集群启动完成，然后执行SQL脚本:"
echo "   flink-sql-client.sh -j $JAR_FILE -f setup.sql"
echo ""
echo "4. 或者直接提交作业:"
echo "   flink run $JAR_FILE"
echo ""
echo "5. 检查作业状态:"
echo "   flink list"
echo "   curl http://192.168.1.100:9097/overview"
echo ""

echo "=== 部署准备完成 ==="
